package com.zzq.guli.service.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzq.guli.service.base.result.R;
import com.zzq.guli.service.edu.entity.Chapter;
import com.zzq.guli.service.edu.entity.ChapterVo;
import com.zzq.guli.service.edu.entity.Video;
import com.zzq.guli.service.edu.service.ChapterService;
import com.zzq.guli.service.edu.service.VideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ZZQ
 * @since 2024-04-12
 */

//@CrossOrigin
@RestController
@RequestMapping("admin/edu/chapter")
public class ChapterController {

    @Autowired
    ChapterService chapterService;

    @Autowired
    VideoService videoService;

    @GetMapping("getNestedChapter/{courseId}")
    public R getChapterList(@PathVariable("courseId") String courseId){

        List<ChapterVo> chapterList=chapterService.getNestedList(courseId);

        return R.ok().data("chapterList",chapterList);


    }



    @RequestMapping("addChapter")
    public R addChapter(@RequestBody Chapter chapter){
        boolean save = chapterService.save(chapter);
        if(save){
            System.out.println("添加成功");
            return R.ok();
        }else {
            System.out.println("添加失败");
            return R.error();
        }
    }

    @RequestMapping("getChapterById/{chapterId}")
    public R getChapterById(@PathVariable String chapterId){
        Chapter byId = chapterService.getById(chapterId);
        return R.ok().data("chapter",byId);
    }


    @RequestMapping("updateChapterById")
    public R updateChapterById(@RequestBody Chapter chapter){
        boolean b = chapterService.updateById(chapter);
        return R.ok().message("更新成功");

    }


    @DeleteMapping("removeById/{chapterId}")
    public R removeById(@PathVariable String chapterId){
       //删除章节信息
        boolean b = chapterService.removeById(chapterId);

        //删除课时信息
        QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("chapter_id",chapterId);
        boolean b1 = videoService.remove(videoQueryWrapper);

        return R.ok().message("删除章节以及课时信息成功");
    }
}

